/* The file is part of the Waff web framework.
 *
 * Copyright (c) 2011, Grzegorz Skorupa
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditionsare met:
 *
 * - Redistributions of source code must retain the above copyright notice, this list of conditions
 *   and the following disclaimer.
 * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions
 *   and the following disclaimer in the documentation and/or other materials provided with the distribution.
 * - Neither the name of the Grzegorz Skorupa nor the names of its contributors may be used to endorse or promote products
 *   derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package pl.org.waff;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Set;
import java.util.logging.Logger;

/**
 *
 * @author greg
 */
public class DefaultNavigator implements SiteNavigator {

    private LinkedHashMap paths = null;
    private String applicationPrefix;
    private static final Logger log = Logger.getLogger(DefaultNavigator.class.getName());

    public void init(Properties properties, String applicationPrefix) {
        paths = new LinkedHashMap();
        this.applicationPrefix = applicationPrefix;
        Set keyset = properties.getKeySet();
        Iterator it = keyset.iterator();
        String pathLocation;
        String value;
        String accessibility = null;
        String name = null;
        String templateLocation = null;
        String mobileTemplateLocation = null;
        int separatorPos = -1;
        int templateSeparatorPos = -1;
        while (it.hasNext()) {
            pathLocation = (String) it.next();
            value = properties.getProperty(pathLocation);
            separatorPos = value.indexOf(":");
            templateSeparatorPos = value.lastIndexOf(":");
            if (separatorPos > 0) {
                if (templateSeparatorPos > 0 && templateSeparatorPos != separatorPos) {
                    name = value.substring(0, separatorPos);
                    accessibility = value.substring(separatorPos + 1, templateSeparatorPos);
                    templateLocation = value.substring(templateSeparatorPos + 1);
                } else {
                    name = value.substring(0, separatorPos);
                    accessibility = value.substring(separatorPos + 1);
                    templateLocation = null;

                }
            } else {
                name = pathLocation;
                accessibility = value;
                templateLocation = null;
            }
            mobileTemplateLocation=createMobileTemplateLocation(templateLocation);
            paths.put(pathLocation, new PathElement(pathLocation, name, accessibility, templateLocation, mobileTemplateLocation, properties));
            if ("/".equals(pathLocation)) {
                paths.put("", new PathElement(pathLocation, name, accessibility, templateLocation, mobileTemplateLocation, properties));
            }
        }
    }
    
    private String createMobileTemplateLocation(String location){
        String result=null;
        if(location==null){
            return null;
        }
        int indexOfSeparator=location.lastIndexOf("/")+1;
        result=location.substring(0, indexOfSeparator)+"m_"+location.substring(indexOfSeparator);
        return result;
    }

    public PathElement getPathData(String pathLocation, String pathPrefix) {
        String path = pathLocation;
        //if mobile devices has specific request processind then pathPrefix is set to not null value
        if (pathPrefix != null && pathPrefix.length() > 0) {
            path = path.substring(pathPrefix.length() - 1);
            if (!path.startsWith("/")) {
                path = "/" + path;
            }
        }
        return (PathElement) paths.get(path);
    }

    public ArrayList getChildren(String parentPath) {
        ArrayList elements = new ArrayList();
        Iterator it = paths.values().iterator();
        PathElement pe;
        String parent;
        String path;
        boolean rootAdded = false;
        while (it.hasNext()) {
            pe = (PathElement) it.next();
            if (pe.isVisible()) {
                parent = pe.getParentPath();
                path = pe.getPath();
                if (parentPath != null) {
                    if (parentPath.equals(parent)) {
                        elements.add(pe);
                    }
                } else {
                    if ("/".equals(parent) || null == parent) {
                        if ("/".equals(path)) {
                            if (!rootAdded) {
                                elements.add(pe);
                                rootAdded = true;
                            }
                        } else {
                            elements.add(pe);
                        }
                    }
                }
            }
        }
        return elements;
    }

    public String getAbsolutePath(String relativePath) {
        return applicationPrefix + relativePath;
    }

    public String getAbsolutePath(String relativePath, String pathPrefix) {
        String correctedPathPrefix=pathPrefix;
        if(correctedPathPrefix!=null && correctedPathPrefix.endsWith("/")){
            correctedPathPrefix=correctedPathPrefix.substring(0, correctedPathPrefix.length()-1);
        }
        if (correctedPathPrefix == null) {
            return applicationPrefix + relativePath;
        } else {
            return applicationPrefix + correctedPathPrefix + relativePath;
        }
    }
}
